package com.estore.service;

import com.estore.model.Product;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * 商品服务类，负责商品相关的业务逻辑
 * 内存实现，后续可无缝切换为数据库
 */
public class ProductService {

    /* ==================== 数据存储 ==================== */
    private final Map<String, Product> productData = new HashMap<>();

    public ProductService() {
        productData.put("p001", new Product("p001", "Java从入门到精通", 99.0, 100));
        productData.put("p002", new Product("p002", "高性能MySQL", 128.5, 50));
        productData.put("p003", new Product("p003", "颈椎康复指南", 45.0, 200));
        productData.put("p004", new Product("p004", "生发秘籍", 88.0, 0));
    }

    /* ==================== 基础 CRUD ==================== */
    public List<Product> getAllProducts() {
        return new ArrayList<>(productData.values());
    }

    public Product findProductById(String id) {
        return productData.get(id);
    }

    public void saveProduct(Product product) {
        if (product == null || product.getId() == null) {
            throw new IllegalArgumentException("商品或商品ID不能为空");
        }
        productData.put(product.getId(), product);
    }

    public boolean deleteProduct(String id) {
        return productData.remove(id) != null;
    }

    public boolean deleteProducts(Collection<String> ids) {
        boolean modified = false;
        for (String id : ids) {
            if (productData.remove(id) != null) modified = true;
        }
        return modified;
    }

    public int countProducts() {
        return productData.size();
    }

    /* ==================== 库存相关 ==================== */
    public boolean reduceStock(String id, int quantity) {
        Product p = findProductById(id);
        if (p == null || p.getStock() < quantity) return false;
        p.setStock(p.getStock() - quantity);
        return true;
    }

    public boolean increaseStock(String id, int quantity) {
        Product p = findProductById(id);
        if (p == null || quantity <= 0) return false;
        p.setStock(p.getStock() + quantity);
        return true;
    }

    public List<Product> getOutOfStockProducts() {
        return productData.values()
                .stream()
                .filter(prod -> prod.getStock() == 0)
                .collect(Collectors.toList());
    }

    /* ==================== 查询增强 ==================== */
    public List<Product> searchByName(String keyword) {
        if (keyword == null || keyword.trim().isEmpty()) return getAllProducts();
        String key = keyword.toLowerCase();
        return productData.values()
                .stream()
                .filter(prod -> prod.getName().toLowerCase().contains(key))
                .collect(Collectors.toList());
    }

    public List<Product> getProductsByPriceRange(double min, double max) {
        return productData.values()
                .stream()
                .filter(prod -> prod.getPrice() >= min && prod.getPrice() <= max)
                .collect(Collectors.toList());
    }
}